/**
 * Created by lwy on 2017/8/30.
 */


var details;
var goodsName;


$(function () {
    var id= $('#idBox').html();
    getCrmDetails(id);
})

function getCrmDetails(id) {
    $.ajax({
        url: '/crm/seckillGoodsC/getCrmDetails.json',
        dataType: 'json',
        data: {
            token: localStorage.getItem('crmtoken'),
            id: id,
        },
        success: function (data) {
            if (data.code == -1) {
                window.parent.location.href = "/crm/login.jsp";
            }
            if (data.code == 0) {
                alert(data.msg);
            }
            if (data.code == 1) {
                var data = data.datas;
                $('#name').val(data.goodsName);
                $('#specificationName').val(data.specificationName);
                $('#salePrice').val(data.salePrice);
                $('#seckillPrice').val(data.seckillPrice);
                $('#saleVolume').val(data.saleVolume);
                $('#inventory').val(data.inventory);
                $('#totalInventory').val(data.totalInventory);
                $('#seckillDate').val(data.seckillDate);
                $('#userName').val(data.userName);
                $('#createTime').val(data.createTime);
                var morePic = data.mainImg.split(',');
                console.log(morePic.length);
                $('#imgBox').empty();
                if(morePic.length>1){
                    for(var i=0;i<morePic.length-1;i++){
                        $('<div class="oneImgBox"><img src="'+morePic[i]+'"/><</div>').appendTo($('#imgBox'))
                    }
                }else{
                    $('<div class="oneImgBox"><img src="'+morePic[0]+'"/></div>').appendTo($('#imgBox'))
                }

                // var ue = UE.getEditor('container', {
                //     autoHeight: false,
                //     initialFrameHeight:800
                // });

                if (data.state == "有效") {
                    $('#xiajia').show();
                    $('#shangjia').hide();
                }
                if (data.state == '无效') {
                    $('#shangjia').show();
                    $('#xiajia').hide();
                }

            }
        }
    })

}


//修改
$('#editAll').click(function () {
    $('#editAll').hide();
    $('#inventory').removeAttr('readonly');
    $('#seckillPrice').removeAttr('readonly');
    $('#save2').show();

})

//保存修改
$('#save2').click(function () {

    $.ajax({
        url: '/crm/seckillGoodsC/edit.json',
        dataType: 'json',
        data: {
            inventory: $('#inventory').val(),
            seckillPrice: $('#seckillPrice').val(),
            id: $('#idBox').html(),
            token: localStorage.getItem('crmtoken'),
        },
        type: 'post',
        success: function (data) {
            if (data.code == 0) {
                alert(data.msg);
            }
            if (data.code == -1) {
                window.parent.location.href = "/crm/login.jsp"
            }
            if (data.code == 1) {
                alert('修改申请成功')
            }
        }
    })
})









//上架
$('#shangjia').click(function () {
    $.ajax({
        url:'/crm/seckillGoodsC/updateState.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            id:$('#idBox').html(),
            state:'true',
        },
        type:'post',
        success:function (data) {
            if(data.code==1){
                alert('上架成功');
                getCrmDetails($('#idBox').html())
            }
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==0){
                alert(data.msg);
            }
        }
    })
})

//下架
$('#xiajia').click(function () {
    $.ajax({
        url:'/crm/seckillGoodsC/updateState.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            id:$('#idBox').html(),
            state:'false',
        },
        type:'post',
        success:function (data) {
            if(data.code==1){
                alert('下架成功');
                getCrmDetails($('#idBox').html())
            }
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==0){
                alert(data.msg);
            }
        }
    })
})

